package com.booth.manage.controllers;

import com.booth.base.controllers.BaseController;
import com.booth.base.model.Users;
import com.booth.base.utils.DateUtil;
import com.booth.base.utils.ResultStatus;
import com.booth.manage.model.BoothProgress;
import com.booth.manage.model.BussOperator;
import com.jfinal.plugin.activerecord.Page;

/***
 * 摊位申请控制器
 * @author admin
 *
 */
public class BoothProgressController extends BaseController {
	private boolean success = false;
	
	private final static String BASE_PAGE_URL = "booth_progress";

	/***
	 * 进入列表页
	 */
	public void index() {
		render(BASE_PAGE_URL+"_index.jsp");
	}
	public void myApply() {
		render(BASE_PAGE_URL+"_my.jsp");
	}
	/***
	 * 根据id是否为空，为空进入新增页，不为空进入修改页
	 */
	public void au_page() {
		Integer selectId = getParaToInt("selectId", 0);
		if (selectId != null && selectId > 0) {
			BussOperator item = BussOperator.dao.findById(selectId);
			setAttr("boothProgress", item);
		}
		render(BASE_PAGE_URL+"_au.jsp");
	}
	
	public void pay_page() {
		Integer selectId = getParaToInt("id", 0);
		setAttr("selectId", selectId);
		render(BASE_PAGE_URL+"_pay.jsp");
	}
	public void doPay() {
		Integer selectId = getParaToInt("id", 0);
		if (selectId > 0) {
			String a = getPara();
			/*System.out.println("a=:"+a);
			Map<String, String[]> params = getParaMap(); 
			System.out.println("b=:"+JSON.toJSONString(params));
			System.out.println("b=:"+JSON.toJSONString(getParams(getParaMap())));
			 */
			success = BoothProgress.dao.doPay(selectId,getParams(getParaMap()), getCurUser());
			if (success){
				renderResult(ResultStatus.SUCCESS);
			}
			
			else
				renderResult(ResultStatus.DELETE_FAILURE);
		}
	}
	public void input_pay_page() {
		Integer selectId = getParaToInt("id", 0);
		setAttr("selectId", selectId);
		setAttr("boothId", getParaToInt("boothId"));
		setAttr("applicantId", getParaToInt("applicantId"));
		render(BASE_PAGE_URL+"_input_pay.jsp");
	}

	/***
	 * 获取列表页数据
	 */
	public void list() {
		String progressName = getPara("progressName");
		String progressStatus = getPara("progressStatus");
		String createStartTime = getPara("createStartTime");
		String createEndTime = getPara("createEndTime");
		String checkStartTime = getPara("checkStartTime");
		String checkEndTime = getPara("checkEndTime");
		String businessStartTime = getPara("businessStartTime");
		String businessEndTime = getPara("businessEndTime");
		Integer flag = getParaToInt("flag",0);
		Users curUser = super.getCurUser();
		Integer applyId = Integer.MIN_VALUE;
		if(curUser.getGroupType() != null &&  curUser.getGroupType() == GROUP_TYPE_3) {
			applyId = curUser.getUserId();
		}
		int page = getParaToInt("page", 1);
		int pageSize = getParaToInt("rows", 10);
		Page<BoothProgress> pager = BoothProgress.dao.findPage(page, pageSize,progressName
				,progressStatus,createStartTime,createEndTime,checkStartTime,checkEndTime,businessStartTime,businessEndTime,applyId,flag);
		setAttr("total", pager.getTotalRow());
		setAttr("rows", pager.getList());
		renderResult(ResultStatus.SUCCESS);
	}
	public void myApplyList() {
		String progressName = getPara("progressName");
		String progressStatus = getPara("progressStatus");
		String createStartTime = getPara("createStartTime");
		String createEndTime = getPara("createEndTime");
		String checkStartTime = getPara("checkStartTime");
		String checkEndTime = getPara("checkEndTime");
		String businessStartTime = getPara("businessStartTime");
		String businessEndTime = getPara("businessEndTime");
		Integer flag = getParaToInt("flag",0);
		Users curUser = super.getCurUser();
		Integer applyId = Integer.MIN_VALUE;
		if(curUser.getGroupType() != null &&  curUser.getGroupType() == GROUP_TYPE_3) {
			applyId = curUser.getUserId();
		}
		int page = getParaToInt("page", 1);
		int pageSize = getParaToInt("rows", 10);
		Page<BoothProgress> pager = BoothProgress.dao.findMyApply(page, pageSize,progressName
				,progressStatus,createStartTime,createEndTime,checkStartTime,checkEndTime,businessStartTime,businessEndTime,applyId,flag);
		setAttr("total", pager.getTotalRow());
		setAttr("rows", pager.getList());
		renderResult(ResultStatus.SUCCESS);
	}

	/***
	 * 根据id是否为空判断为新增还是修改，为空则为新增，不为空则为修改
	 */
	public void save() {
		Users u = getCurUser();
		BoothProgress item = getModel(BoothProgress.class);
		item.set("business_start_time", DateUtil.toDateStr(getParaToDate("business_start_time"), DateUtil.YMHMS));
		item.set("business_end_time", DateUtil.toDateStr(getParaToDate("business_end_time"), DateUtil.YMHMS));
		item.set("sublet_start_time", DateUtil.toDateStr(getParaToDate("business_start_time"), DateUtil.YMHMS));
		item.set("sublet_end_time", DateUtil.toDateStr(getParaToDate("business_end_time"), DateUtil.YMHMS));
		if (item.getInt("id") == null || item.getInt("id") == 0) {
			setCreator(item, u);
			item.set("user_id", u.getUserId());
			item.set("user_name", u.getUserName());
			success = item.save();
			if (success){
				
				renderResult(ResultStatus.SUCCESS);
			}
			else
				renderResult(ResultStatus.ADD_FAILURL);
		} else {
			setOperator(item, u);
			success = item.update();
			if (success)
				renderResult(ResultStatus.SUCCESS);
			else
				renderResult(ResultStatus.UPDATE_FAILURE);
		}
		
	}

	/***
	 * 删除
	 */
	public void doProgressStatus() {
		Integer selectId = getParaToInt("selectId", 0);
		if (selectId > 0) {
			
			success = BoothProgress.dao.doProgressStatus(selectId,getParaToInt("status"), getCurUser());
			if (success){
				renderResult(ResultStatus.SUCCESS);
			}
				
			else
				renderResult(ResultStatus.DELETE_FAILURE);
		}
	}

	

}
